package cn.mumzone.pos.api;

import cn.mumzone.basic.net.http.ApiRequestHelper;
import cn.mumzone.basic.net.http.interceptor.HttpLoggingInterceptor;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.api.service.AppService;
import cn.mumzone.pos.api.service.GoodsService;
import cn.mumzone.pos.api.service.MemberService;
import cn.mumzone.pos.api.service.ReportService;
import cn.mumzone.pos.api.service.ScheduleService;
import cn.mumzone.pos.api.service.SettleService;
import cn.mumzone.pos.api.service.StockService;
import cn.mumzone.pos.api.service.UserService;
import okhttp3.Interceptor;

/**
 * Author Chaney
 * Date   2017/3/6 下午9:57.
 * EMail  qiuzhenhuan.hi@gmail.com
 */

public class ApiRequest {
    private static ApiRequestHelper apiRequestHelper;
    private static AppService appService;
    private static BillService billService;
    private static UserService userService;
    private static ReserveService reserveService;
    private static GoodsService goodsService;
    private static MemberService memberService;
    private static SettleService settleService;
    private static ReportService reportService;
    private static ScheduleService scheduleService;
    private static StockService stockService;

    public static void init(String baseUrl) {
        apiRequestHelper = ApiRequestHelper.getInstance();
        apiRequestHelper.setBaseUrl(baseUrl);
        // TODO: 2019/6/27 修改拦截器设置方式？
        boolean isAddedInterceptor = false;

        if (!ListUtil.isListEmpty(apiRequestHelper.getOkHttpClient().interceptors())) { //避免登录后重设拦截器
            for (Interceptor interceptor : apiRequestHelper.getOkHttpClient().interceptors()) {
                if (interceptor instanceof HeaderInterceptor) {
                    isAddedInterceptor = true;
                    break;
                }
            }
        }

        if (!isAddedInterceptor) {
            apiRequestHelper.setOkHttpClient(apiRequestHelper.getOkHttpClient().newBuilder()
                    .addInterceptor(new HeaderInterceptor())
                    .addInterceptor(new HttpLoggingInterceptor())
                    .build());
        }

        userService = apiRequestHelper.createService(UserService.class);
        appService = apiRequestHelper.createService(AppService.class);
        reserveService = apiRequestHelper.createService(ReserveService.class);
        userService = apiRequestHelper.createService(UserService.class);
        billService = apiRequestHelper.createService(BillService.class);
        memberService = apiRequestHelper.createService(MemberService.class);
        settleService = apiRequestHelper.createService(SettleService.class);
        scheduleService = apiRequestHelper.createService(ScheduleService.class);
        reportService = apiRequestHelper.createService(ReportService.class);
        goodsService = apiRequestHelper.createService(GoodsService.class);
        stockService = apiRequestHelper.createService(StockService.class);
    }

    public static void refreshUrl(String baseUrl) {
        apiRequestHelper.setBaseUrl(baseUrl);
    }


    public static AppService getAppService() {
        return appService;
    }

    public static UserService getUserService() {
        return userService;
    }

    public static ReserveService getReserveService() {
        return reserveService;
    }

    public static MemberService getMemberService() {
        return memberService;
    }

    public static SettleService getSettleService() {
        return settleService;
    }

    public static BillService getBillService() {
        return billService;
    }

    public static ScheduleService getScheduleService() {
        return scheduleService;
    }

    public static ReportService getReportService() {
        return reportService;
    }

    public static GoodsService getGoodsService() {
        return goodsService;
    }

    public static StockService getStockService() {
        return stockService;
    }
}
